//
//  ViewController.m
//  Send_Email
//
//  Created by hanxiao on 2017/6/22.
//  Copyright © 2017年 hanxiao. All rights reserved.
//

#import "ViewController.h"
@import MessageUI;
@interface ViewController ()<MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate>



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//发送邮件
- (IBAction)sendemailAction:(id)sender {
    
    if ([MFMailComposeViewController canSendMail] == YES) {
        
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
        //  设置代理(与以往代理不同,不是"delegate",千万不能忘记呀,代理有3步)
        mailVC.mailComposeDelegate = self;
        //  收件人
        NSArray *sendToPerson = @[@"1559965862@qq.com"];
        [mailVC setToRecipients:sendToPerson];
        //  抄送
        NSArray *copyToPerson = @[@"1325329596@qq.com"];
        [mailVC setCcRecipients:copyToPerson];
        //  密送
        NSArray *secretToPerson = @[@"1325329596@qq.com"];
        [mailVC setBccRecipients:secretToPerson];
        //  主题
        [mailVC setSubject:@"hello world"];
        [self presentViewController:mailVC animated:YES completion:nil];
        [mailVC setMessageBody:@"魑魅魍魉,手机发送的测试数据" isHTML:NO];
    }else{
        
        NSLog(@"此设备不支持邮件发送");
        
    }
    
    
    
}
//邮件delegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error{
    
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"取消发送");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"发送失败");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"保存草稿文件");
            break;
        case MFMailComposeResultSent:
            NSLog(@"发送成功");
            break;
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

//发送短信
- (IBAction)SendMessageAction:(id)sender {
    
    //首先判断当前设备是否可以发送短信
    if ([MFMessageComposeViewController canSendText]) {
        
        MFMessageComposeViewController *message = [[MFMessageComposeViewController alloc]init];
        
        //设置委托
        message.messageComposeDelegate = self;
        
        //短信内容
        message.body = @"哇哦，你懂的";
        
        //设置短信收件方
        message.recipients = @[@"15900634314"];
        
        //显示发送界面
        [self presentViewController:message animated:YES completion:nil];
        
    }
    else{
        
        NSLog(@"此设备不支持短信发送");
    }
    
}

//发送短信delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    switch (result) {
        case MessageComposeResultSent:
            NSLog(@"发送成功");
            break;
        case MessageComposeResultFailed:
            NSLog(@"发送失败");
            break;
        case MessageComposeResultCancelled:
            NSLog(@"取消发送");
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}



@end
